<!-- 左侧搜索Tab -->
<template>
  <div class="container-box">
    <div class="title-box">{{ title }}</div>
    <div class="content-box">
      <div class="search-box">
        <a-input ref="userNameInput" v-model="curValue" placeholder="搜索" @input="currentValueChange">
          <a-icon slot="prefix" type="search" />
        </a-input>
      </div>
      <div class="tab-box">
        <div
          v-for="(item, index) in tabList"
          :key="index"
          :class="['tab-item', item.isSelected ? 'selected' : '']"
          @click="onTabClick(item)">
          {{ item.label }}
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { debounce } from '@/utils/util.js';

export default {
  name: 'SearchLeftTab',
  props: {
    title: {
      type: String,
      default: ''
    },
    searchTabList: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      curValue: '',
      tabList: [],
      originalTabList: []
    }
  },
  mounted() {
    this.originalTabList = this.searchTabList;
    this.getTabList();
  },
  methods: {
    getTabList() {
      this.tabList = this.originalTabList.map(item => {
        return { ...item, isSelected: false };
      });
    },
    // 查询
    onSearch() {
      if (this.curValue) {
        this.tabList = this.originalTabList.filter(item => item.label === this.curValue);
      } else {
        this.tabList = this.originalTabList;
      }
      this.tabList.forEach(item => item.isSelected = false);
      this.$emit('successCallback');
    },
    currentValueChange: debounce(function() {
      this.onSearch();
    }, 1000),
    onTabClick(item) {
      this.tabList.forEach(item => item.isSelected = false);
      item.isSelected = true;
      this.$emit('successCallback', item);
    }
  }
}
</script>

<style lang="less" scoped>
.container-box {

}
.title-box {
  padding: 10px 16px;
  background-color: rgba(239, 246, 253, 1);
}
.content-box {
  border: 1px solid rgba(187, 187, 187, 1);
  border-top: 0;
  .search-box {
    padding: 10px 16px;
  }
  .tab-box {
    width: 240px;
    max-height: 70vh;
    overflow-y: auto;
    .tab-item {
      padding: 10px 13px;
      background-color: #fff;
      color: rgba(16, 16, 16, 1);
      cursor: pointer;
    }
    .selected {
      background-color: rgba(240, 250, 255, 1);
    }
  }
}
</style>